package com.account.service;

import com.account.dao.StorageDao;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seata.api.StorageService;
import com.seata.entity.Storage;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * @作者: 林江
 * @创建时间: 2023/12/5
 * @功能:
 */
@Slf4j
@DubboService
public class StorageServiceImpl implements StorageService {
    @Resource
    StorageDao storageDao;

    @Override
    public void deduct(String commodityCode, int count) {
        log.info("扣除存储数量。。。。{},全局事务id： {},分支事务类型：{}", count, RootContext.getXID(), RootContext.getBranchType());
        Storage storage = storageDao.selectOne(new LambdaQueryWrapper<Storage>()
                .eq(Storage::getCommodityCode, commodityCode));
        if (storage.getCount() == 0) {
            throw new RuntimeException("没有库存了。。。");
        }
        storage.setCount(storage.getCount() - count);
        // 扣减库存
        storageDao.updateById(storage);
    }

}
